
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>Metrics Servlets | Metrics</title>
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css"/>
    <link rel="stylesheet" href="../../_static/metrics.css" type="text/css"/>
    <link rel="top" title="Metrics" href="../../"/>
    <link rel="up" title="User Manual" href="../"/>
    <link rel="next" title="Instrumenting Web Applications" href="../servlet/"/>
    <link rel="prev" title="JSON Support" href="../json/"/>
    <style lang="text/css">
        #top-bar, #top-bar small, #top-bar a {
            text-shadow: 0px -1px 0px #cf2c0f;
            color: #fff;
        }
        
        #top-bar {
            background-color: #ED4A2D;
            background-image: -moz-linear-gradient(top, #ff684b, #cf2c0f);
            background-image: -ms-linear-gradient(top, #ff684b, #cf2c0f);
            background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#ff684b), to(#cf2c0f));
            background-image: -webkit-linear-gradient(top, #ff684b, #cf2c0f);
            background-image: -o-linear-gradient(top, #ff684b, #cf2c0f);
            background-image: linear-gradient(top, #ff684b, #cf2c0f);
            filter: progid:DXImageTransform.Microsoft.gradient(startColorstr = '#ff684b', endColorstr = '#cf2c0f', GradientType = 0);
            -webkit-border-radius: 0;
            -moz-border-radius: 0;
            border-radius: 0;
        }


        .hero-unit {
            background-image: url("../../_static/metrics-hat.png") !important;
            background-repeat: no-repeat !important;
            background-position: 30px 50px;
        }

        .hero-unit div.section {
            padding-left: 200px !important;
        }
    </style>
</head>
<body>
    <a href="https://github.com/dropwizard/metrics">
        <img style="position: absolute; top: 0; right: 0; border: 0;"
             src=""
             alt="Fork me on GitHub"></a>
    <div class="navbar">
        <div class="navbar-inner container-fluid" id="top-bar">
            <header class="row-fluid">
                <h1 class="span12" id="title"><img class="logo" src="../../_static/metrics-logo.png" alt="Logo"/>
                    <a href="../../">Metrics</a>
                    <small>Mind the gap.</small>
                </h1>
            </header>
        </div>
    </div>
    <div class="container-fluid">
        <div class="row-fluid">
            <div class="span3" id="sidebar">
                <ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../../getting-started/">Getting Started</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../">User Manual</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="../core/">Metrics Core</a></li>
<li class="toctree-l2"><a class="reference internal" href="../healthchecks/">Health Checks</a></li>
<li class="toctree-l2"><a class="reference internal" href="../ehcache/">Instrumenting Ehcache</a></li>
<li class="toctree-l2"><a class="reference internal" href="../ganglia/">Reporting to Ganglia</a></li>
<li class="toctree-l2"><a class="reference internal" href="../graphite/">Reporting to Graphite</a></li>
<li class="toctree-l2"><a class="reference internal" href="../httpclient/">Instrumenting Apache HttpClient</a></li>
<li class="toctree-l2"><a class="reference internal" href="../jdbi/">Instrumenting JDBI</a></li>
<li class="toctree-l2"><a class="reference internal" href="../jersey/">Instrumenting Jersey</a></li>
<li class="toctree-l2"><a class="reference internal" href="../jetty/">Instrumenting Jetty</a></li>
<li class="toctree-l2"><a class="reference internal" href="../log4j/">Instrumenting Log4j</a></li>
<li class="toctree-l2"><a class="reference internal" href="../logback/">Instrumenting Logback</a></li>
<li class="toctree-l2"><a class="reference internal" href="../jvm/">JVM Instrumentation</a></li>
<li class="toctree-l2"><a class="reference internal" href="../json/">JSON Support</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="">Metrics Servlets</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#healthcheckservlet">HealthCheckServlet</a></li>
<li class="toctree-l3"><a class="reference internal" href="#threaddumpservlet">ThreadDumpServlet</a></li>
<li class="toctree-l3"><a class="reference internal" href="#metricsservlet">MetricsServlet</a></li>
<li class="toctree-l3"><a class="reference internal" href="#pingservlet">PingServlet</a></li>
<li class="toctree-l3"><a class="reference internal" href="#adminservlet">AdminServlet</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../servlet/">Instrumenting Web Applications</a></li>
<li class="toctree-l2"><a class="reference internal" href="../third-party/">Third Party Libraries</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../about/">About Metrics</a></li>
</ul>

                <hr/>
                <ul>
                    <li><a href="https://dropwizard.github.io/metrics/3.0.2/apidocs/">Javadoc</a></li>
                    <li><a href="https://groups.google.com/forum/#!forum/metrics-user">Mailing List</a></li>
                </ul>
            </div>
            <div class="span9" id="body">
                
  <div class="section" id="metrics-servlets">
<span id="manual-servlets"></span><h1>Metrics Servlets</h1>
<p>The <tt class="docutils literal"><span class="pre">metrics-servlets</span></tt> module provides a handful of useful servlets:</p>
<div class="section" id="healthcheckservlet">
<span id="man-servlet-healthcheck"></span><h2>HealthCheckServlet</h2>
<p><tt class="docutils literal"><span class="pre">HealthCheckServlet</span></tt> responds to <tt class="docutils literal"><span class="pre">GET</span></tt> requests by running all the [health checks](#health-checks)
and returning <tt class="docutils literal"><span class="pre">501</span> <span class="pre">Not</span> <span class="pre">Implemented</span></tt> if no health checks are registered, <tt class="docutils literal"><span class="pre">200</span> <span class="pre">OK</span></tt> if all pass, or
<tt class="docutils literal"><span class="pre">500</span> <span class="pre">Internal</span> <span class="pre">Service</span> <span class="pre">Error</span></tt> if one or more fail. The results are returned as a human-readable
<tt class="docutils literal"><span class="pre">text/plain</span></tt> entity.</p>
<p><tt class="docutils literal"><span class="pre">HealthCheckServlet</span></tt> requires that the servlet context has a <tt class="docutils literal"><span class="pre">HealthCheckRegistry</span></tt> named
<tt class="docutils literal"><span class="pre">com.codahale.metrics.servlets.HealthCheckServlet.registry</span></tt>. You can subclass
<tt class="docutils literal"><span class="pre">MetricsServletContextListener</span></tt>, which will add a specific <tt class="docutils literal"><span class="pre">HealthCheckRegistry</span></tt> to the servlet
context.</p>
</div>
<div class="section" id="threaddumpservlet">
<span id="man-servlet-threaddump"></span><h2>ThreadDumpServlet</h2>
<p><tt class="docutils literal"><span class="pre">ThreadDumpServlet</span></tt> responds to <tt class="docutils literal"><span class="pre">GET</span></tt> requests with a <tt class="docutils literal"><span class="pre">text/plain</span></tt> representation of all the live
threads in the JVM, their states, their stack traces, and the state of any locks they may be
waiting for.</p>
</div>
<div class="section" id="metricsservlet">
<span id="man-servlet-metrics"></span><h2>MetricsServlet</h2>
<p><tt class="docutils literal"><span class="pre">MetricsServlet</span></tt> exposes the state of the metrics in a particular registry as a JSON object.</p>
<p><tt class="docutils literal"><span class="pre">MetricsServlet</span></tt> requires that the servlet context has a <tt class="docutils literal"><span class="pre">MetricRegistry</span></tt> named
<tt class="docutils literal"><span class="pre">com.codahale.metrics.servlets.MetricsServlet.registry</span></tt>. You can subclass
<tt class="docutils literal"><span class="pre">MetricsServletContextListener</span></tt>, which will add a specific <tt class="docutils literal"><span class="pre">MetricRegistry</span></tt> to the servlet
context.</p>
<p><tt class="docutils literal"><span class="pre">MetricsServlet</span></tt> also takes an initialization parameter, <tt class="docutils literal"><span class="pre">show-jvm-metrics</span></tt>, which if <tt class="docutils literal"><span class="pre">&quot;false&quot;</span></tt> will
disable the outputting of JVM-level information in the JSON object.</p>
</div>
<div class="section" id="pingservlet">
<span id="man-servlet-ping"></span><h2>PingServlet</h2>
<p><tt class="docutils literal"><span class="pre">PingServlet</span></tt> responds to <tt class="docutils literal"><span class="pre">GET</span></tt> requests with a <tt class="docutils literal"><span class="pre">text/plain</span></tt>/<tt class="docutils literal"><span class="pre">200</span> <span class="pre">OK</span></tt> response of <tt class="docutils literal"><span class="pre">pong</span></tt>. This is
useful for determining liveness for load balancers, etc.</p>
</div>
<div class="section" id="adminservlet">
<span id="man-servlet-admin"></span><h2>AdminServlet</h2>
<p><tt class="docutils literal"><span class="pre">AdminServlet</span></tt> aggregates <tt class="docutils literal"><span class="pre">HealthCheckServlet</span></tt>, <tt class="docutils literal"><span class="pre">ThreadDumpServlet</span></tt>, <tt class="docutils literal"><span class="pre">MetricsServlet</span></tt>, and
<tt class="docutils literal"><span class="pre">PingServlet</span></tt> into a single, easy-to-use servlet which provides a set of URIs:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">/</span></tt>: an HTML admin menu with links to the following:<ul>
<li><tt class="docutils literal"><span class="pre">/healthcheck</span></tt>: <tt class="docutils literal"><span class="pre">HealthCheckServlet</span></tt></li>
<li><tt class="docutils literal"><span class="pre">/metrics</span></tt>: <tt class="docutils literal"><span class="pre">MetricsServlet</span></tt></li>
<li><tt class="docutils literal"><span class="pre">/ping</span></tt>: <tt class="docutils literal"><span class="pre">PingServlet</span></tt></li>
<li><tt class="docutils literal"><span class="pre">/threads</span></tt>: <tt class="docutils literal"><span class="pre">ThreadDumpServlet</span></tt></li>
</ul>
</li>
</ul>
<p>You will need to add your <tt class="docutils literal"><span class="pre">MetricRegistry</span></tt> and <tt class="docutils literal"><span class="pre">HealthCheckRegistry</span></tt> instances to the servlet
context as attributes named <tt class="docutils literal"><span class="pre">com.codahale.metrics.servlets.MetricsServlet.registry</span></tt> and
<tt class="docutils literal"><span class="pre">com.codahale.metrics.servlets.HealthCheckServlet.registry</span></tt>, respectively. You can do this using
the Servlet API by extending <tt class="docutils literal"><span class="pre">AdminServletContextListener</span></tt>:</p>
<div class="highlight-java"><div class="highlight"><pre><span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyAdminServletContextListener</span> <span class="kd">extends</span> <span class="n">AdminServletContextListener</span> <span class="o">{</span>
    <span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">MetricRegistry</span> <span class="n">METRIC_REGISTRY</span> <span class="o">=</span> <span class="k">new</span> <span class="n">MetricRegistry</span><span class="o">();</span>
    <span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">HealthCheckRegistry</span> <span class="n">HEALTH_CHECK_REGISTRY</span> <span class="o">=</span> <span class="k">new</span> <span class="n">HealthCheckRegistry</span><span class="o">();</span>

    <span class="nd">@Override</span>
    <span class="kd">protected</span> <span class="n">MetricRegistry</span> <span class="nf">getMetricRegistry</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">return</span> <span class="n">METRIC_REGISTRY</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="nd">@Override</span>
    <span class="kd">protected</span> <span class="n">HealthCheckRegistry</span> <span class="nf">getHealthCheckRegistry</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">return</span> <span class="n">HEALTH_CHECK_REGISTRY</span><span class="o">;</span>
    <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
</div>
</div>


            </div></div>
        <hr/>
        <footer>
            &copy; Copyright 2010-2014, Coda Hale, Yammer Inc..
            Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>
            1.2.2.
        </footer>
    </div>
</body>
</html>


